package com.jqh.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jqh.demo.entity.AclRole;
import com.jqh.demo.entity.AclUser;
import com.jqh.demo.entity.AclUserRole;
import com.jqh.demo.service.AclRoleService;
import com.jqh.demo.service.AclUserRoleService;
import com.jqh.demo.service.AclUserService;
import com.jqh.demo.utils.Judge;
import com.jqh.demo.utils.MD5;
import com.jqh.demo.utils.R;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jqh
 * @since 2021-05-08
 */
@RestController
@RequestMapping("/api/acl-user")
@CrossOrigin
public class AclUserController {
    @Autowired
    private AclUserService aclUserService;

    @Autowired
    private AclUserRoleService aclUserRoleService;

    @Autowired
    private AclRoleService aclRoleService;



    @ApiOperation("用户添加")
    @PostMapping("addUser")
    public R addUser(@RequestBody AclUser aclUser){
        if (StringUtils.isEmpty(aclUser.getUserName())&&StringUtils.isEmpty(aclUser.getPassword())){
            Judge.judge(false,"用户参数为空!");
        }
        //密码加密
        aclUser.setPassword(MD5.encrypt(aclUser.getPassword()));
        boolean flag = aclUserService.save(aclUser);
        //添加默认菜单
        AclRole role = aclRoleService.getOne(new QueryWrapper<AclRole>().eq("role_name", "默认菜单"));
        aclUserRoleService.save(new AclUserRole().setRoleId(role.getId()).setUserId(aclUser.getId()));
        return Judge.judge(flag,"保存失败！");
    }

    @ApiOperation("用户删除及删除与角色的关联")
    @DeleteMapping("{id}")
    public R delUser(@PathVariable String id){
        if(StringUtils.isEmpty(id)){
            return Judge.judge(false,"删除参数为空!");
        }
        boolean flag=aclUserService.delUserWithRelation(id);
        return Judge.judge(flag,"删除失败！");
    }
    @ApiOperation(value = "根据id批量删除用户")
    @DeleteMapping("batchDelUser")
    public R batchDelUser(@RequestBody List<String> idList) {
        if (idList.size() == 0) {
            Judge.judge(false,"删除序号集为空!");
        }
        boolean flag=aclUserService.removeByIdsWithMid(idList);
        return Judge.judge(flag,"批量删除失败！");
    }

    @ApiOperation("更新用户信息")
    @PutMapping("updUser")
    public R updUser(@RequestBody AclUser aclUser){
        if(StringUtils.isEmpty(aclUser.getId())){
            Judge.judge(false,"用户参数Id为空!");
        }
        //密码加密
        aclUser.setPassword(MD5.encrypt(aclUser.getPassword()));
        boolean flag = aclUserService.updateById(aclUser);
        return Judge.judge(flag,"更新失败！");
    }

    @ApiOperation("条件分页查询用户")
    @GetMapping("pageUser/{current}/{limit}")
    public R pageUser(@PathVariable Long current,@PathVariable Long limit,@RequestParam(value="userName",required=false) String userName){
        Page<AclUser> page=new Page<>(current,limit);
        aclUserService.pageQuery(page,userName);
        return R.ok().data("row",page.getRecords()).data("total",page.getTotal());
    }

    @ApiOperation("根据id查询用户信息")
    @GetMapping("{id}")
    public R getUserInfoById(@PathVariable String id){
        if(StringUtils.isEmpty(id)){
            Judge.judge(false,"用户参数Id为空!");
        }
        AclUser aclUser = aclUserService.getById(id);
        return R.ok().data("aclUser",aclUser);
    }



}

